using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace HerenityCode.Reference
{
    namespace Raycast
    {
        /// <summary>
        /// Description:射线相关函数
        /// Author:哒先生
        /// Date:2024年10月9日16:28:30
        /// </summary>
        public class GetTextureColor : MonoBehaviour
        {
            /// <summary>
            /// 获取当前碰撞点的颜色
            /// </summary>
            /// <param name="hit">射线</param>
            /// <param name="texture">颜色贴图</param>
            /// <returns></returns>
            public static Color GetAreaColor(RaycastHit hit,Texture texture)
            {
                if (hit.collider == null) return Color.white;
                Vector2 textureCoord = hit.textureCoord;
                Renderer renderer = hit.collider.GetComponent<Renderer>();
                if (renderer != null)
                {
                    if (texture is Texture2D)
                    {
                        Texture2D texture2D = (Texture2D)texture;
                        float textureX = textureCoord.x * texture2D.width;
                        float textureY = textureCoord.y * texture2D.height;
                        Color pixelColor = texture2D.GetPixel((int)textureX, (int)textureY);
                        if (pixelColor != Color.white)
                        {
                            return pixelColor;
                        }
                    }
                }
                return Color.white;
            }
            
            /// <summary>
            /// 通过事件系统获取射线，计算当前碰撞点颜色
            /// </summary>
            /// <param name="texture">颜色贴图</param>
            /// <returns></returns>
            public static Color GetAreaColor(Texture texture,PointerEventData pointerEventData)
            {
                Ray ray = Camera.main.ScreenPointToRay(pointerEventData.position);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider == null) return Color.white;
                    Vector2 textureCoord = hit.textureCoord;
                    Renderer renderer = hit.collider.GetComponent<Renderer>();
                    if (renderer != null)
                    {
                        if (texture is Texture2D)
                        {
                            Texture2D texture2D = (Texture2D)texture;
                            float textureX = textureCoord.x * texture2D.width;
                            float textureY = textureCoord.y * texture2D.height;
                            Color pixelColor = texture2D.GetPixel((int)textureX, (int)textureY);
                            if (pixelColor != Color.white)
                            {
                                return pixelColor;
                            }
                        }
                    }
                }
                return Color.white;
            }
            
            /// <summary>
            /// 判断两个颜色是否相似
            /// </summary>
            /// <param name="color1">颜色1</param>
            /// <param name="color2">颜色2</param>
            /// <param name="threshold">颜色在色盘上的距离，小于此值代表颜色接近，默认0.005f</param>
            /// <returns></returns>
            public static bool ColorsAreClose(Color color1, Color color2, float threshold = 0.005f)
            {
                float sqrDistance = (color1.r - color2.r) * (color1.r - color2.r) +
                                    (color1.g - color2.g) * (color1.g - color2.g) +
                                    (color1.b - color2.b) * (color1.b - color2.b);
                return sqrDistance < threshold * threshold;
            }
        }
    }
}
